package uit.nfc.utils;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONParser {
	
	public static ArrayList<DishesItem> parseAllDishesItems(String response) {
		ArrayList<DishesItem> dishesItems = null;
		
		if (response != null) {
			JSONObject json = null;
			try {
				json = new JSONObject(response);
				JSONArray jsonArray = json.getJSONArray("item");
				int length = jsonArray.length();
				dishesItems = new ArrayList<DishesItem>(length);
				
				JSONObject jsonTemp = null;
				DishesItem dishesTemp = null;
				
				for (int i = 0; i < length; ++i) {
					jsonTemp = jsonArray.getJSONObject(i);
					dishesTemp = new DishesItem();
					dishesTemp.setId(jsonTemp.getString("itemID"));
					dishesTemp.setName(jsonTemp.getString("itemName"));
					dishesTemp.setPrice(jsonTemp.getString("price"));
					dishesTemp.setImageLink(jsonTemp.getString("imageLink"));
					dishesTemp.setDescription(jsonTemp.getString("description"));
					
					dishesItems.add(dishesTemp);
				}
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		return dishesItems;
	}
	
	public static DishesItem parseDishesItem(String response) {
		DishesItem dishesItem = null;
		
		if (response != null) {
			JSONObject json = null;
			dishesItem = new DishesItem();
			try {
				json = new JSONObject(response);
				
				dishesItem.setId(json.getString("itemID"));
				dishesItem.setName(json.getString("itemName"));
				dishesItem.setPrice(json.getString("price"));
				dishesItem.setImageLink(json.getString("imageLink"));
				dishesItem.setDescription(json.getString("description"));
						
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		return dishesItem;
	}
}
